const {User} = require('@/mysql/user')
const {Project} = require('@/mysql/project')
const {Share} = require('@/mysql/share')
/** 查找查看项目所有分享信息 */
const getAllShare = async ctx => {
    const project_id = ctx.params.id
    ctx.body =await Share.getAllShare(project_id)
}
const  insertShare = async ctx => {
    if (ctx.request.body) {
        const project_id = ctx.params.id
        let {name, description} = ctx.request.body
        if (!name) return ctx.response.body = global.toJson(1, `缺少分享名称`)
        if (project_id) {
            const data = await Project.getById(project_id);
            if(!data.length) return ctx.response.body = global.toJson(1, `没有找到该分享项目`)
        }
        const data = await Share.insertData(name, description, project_id)
        if (data) {
            ctx.response.body = global.toJson(0, '添加分享信息成功')
        }
    }
}
// 修改分享
const updateShare = async ctx => {
    const params = ctx.request.body
    if(params) {
        let {name, description, id} = params
        const data = await Share.updateData(id,name,description)
        if(data.length !== 0) ctx.body = global.toJson(0, `修改分享完成`)
    }
}
// 删除分享
const removeShare = async ctx => {
    const removeId = ctx.params.id
    if(!removeId) ctx.body = global.toJson(1, `没有分享Id,无法删除`)
    const data =  await Share.update({
        deleted: true
    },{where:{id:removeId}})
    if(data.length !== 0) ctx.body = global.toJson(0, '删除成功')
}
export {getAllShare, insertShare, updateShare, removeShare}
